<?php
// +----------------------------------------------------------------------
// | CoreThink [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.corethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com> <http://www.corethink.cn>
// +----------------------------------------------------------------------
namespace Common\Model;
use Think\Model;
/**
 * 用户模型
 * @author jry <598821125@qq.com>
 */
class MemberModel extends Model{
    /**
     * 自动验证规则
     * @author jry <598821125@qq.com>
     */
    protected $_validate = array(
      

        //验证邮箱
        //array('email', 'email', '邮箱格式不正确', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
        array('email', '1,32', '邮箱长度为1-32个字符', self::EXISTS_VALIDATE, 'length', self::MODEL_BOTH),
        array('email', '', '邮箱被占用', self::EXISTS_VALIDATE, 'unique', self::MODEL_BOTH),

        //验证手机号码
        array('tel', '', '手机号被占用', self::EXISTS_VALIDATE, 'unique', self::MODEL_BOTH),

        //验证密码
        array('pwd', 'require', '密码不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_INSERT),
        array('pwd', '6,30', '密码长度为6-30位', self::MUST_VALIDATE, 'length', self::MODEL_INSERT),
        
        array('repwd', 'pwd', '两次输入的密码不一致', self::EXISTS_VALIDATE, 'confirm', self::MODEL_INSERT),

        //验证用户名
        array('name', 'require', '用户名不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('name', '3,32', '用户名长度为1-32个字符', self::MUST_VALIDATE, 'length', self::MODEL_BOTH),
        array('name', '', '用户名被占用', self::MUST_VALIDATE, 'unique', self::MODEL_BOTH),
        

        //重置密码时自动验证规则
        array('email', '1,32', '邮箱长度为1-32个字符', self::EXISTS_VALIDATE, 'length', 5),
        array('pwd', 'require', '密码不能为空', self::EXISTS_VALIDATE, 'regex', 5),
        array('pwd', '6,30', '密码长度为6-30位', self::EXISTS_VALIDATE, 'length', 5),
    );

    /**
     * 自动完成规则
     * @author jry <598821125@qq.com>
     */
    protected $_auto = array(
        array('pwd', 'user_md5', 3, 'function'),
    );

    

   

    
    /**
     * 用户登录
     * @author jry <598821125@qq.com>
     */
    public function login($name, $pwd, $map){
        //去除前后空格
        $map['name'] = trim($name);

        
       
        $user = $this->where($map)->find(); //查找用户
        if(!$user){
            $this->error = '用户不存在！';
        }else{
            if(user_md5($pwd) !== $user['pwd']){
                $this->error = '密码错误！';
            }else{
                //更新登录信息
                session_start();
                $_SESSION["islogin"]=1;
                $_SESSION["username"]=$user['name'];
                $_SESSION["id"]=$user['id'];
                $data = array(
                    'id'              => $user['id'],
                );
                $this->autoLogin($user);
                return $user['id'];
            }
        }
        return false;
    }

    /**
     * 设置登录状态
     * @author jry <598821125@qq.com>
     */
    public function autoLogin($user){
        //记录登录SESSION和COOKIES
        $auth = array(
            'uid'             => $user['id'],
            'name'        => $user['name'],
        );
        session('member_auth', $auth);
        session('member_auth_sign', $this->dataAuthSign($auth));
    }

 /**
     * 数据签名认证
     * @param  array  $data 被认证的数据
     * @return string       签名
     * @author jry <598821125@qq.com>
     */
    public function dataAuthSign($data){
        //数据类型检测
        if(!is_array($data)){
            $data = (array)$data;
        }
        ksort($data); //排序
        $code = http_build_query($data); //url编码并生成query字符串
        $sign = sha1($code); //生成签名
        return $sign;
    }
   

   
    /**
     * 检测用户是否登录
     * @return integer 0-未登录，大于0-当前登录用户ID
     * @author jry <598821125@qq.com>
     */
    public function isLogin(){
        $user = session('member_auth');
        if(empty($user)){
            return 0;
        }else{
            return session('member_auth_sign') == $this->dataAuthSign($user) ? $user['uid'] : 0;
        }
    }
}
